-
-
Notifications
You must be signed in to change notification settings - Fork 430
feat: create new SafeDeclareStrictTypesRector #7814
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
dac996f to
e51df27
Compare
|
Hi, thanks for such a fast kick off. Wow! Before I review, can you use a new Rector rule, e.g. Safe declare...Rector? People are used to current scope of the rule and ignore it as not safe. |
they should not be ignoring it because it's not part of any default set to ignore---if they wanted to use it then they would have needed to manually include it in their configs However, I can certainly create a new rule if you still think it's needed. |
|
Indeed, but devs are lazy and it has a bad karma already. I would not recommend it to anyone :) Could you rename it? |
e51df27 to
fa3c381
Compare
|
@TomasVotruba, done! |
fa3c381 to
743d9a0
Compare
...ation/Rector/StmtsAwareInterface/SafeDeclareStrictTypesRector/Fixture/closure_return.php.inc
Show resolved
Hide resolved
743d9a0 to
6f90d8b
Compare
Hello!
Motivation
See https://x.com/VotrubaT/status/2008248184540418398
Currently, the
DeclareStrictTypesRectorjust indiscriminately addsdeclare(strict_types=1)to all files. However:This PR creates a new rule (
SafeDeclareStrictTypesRector) that is (should be) completely safe and only adds strict types to the file when it won't break code. I've also added it to thecodeQualityruleset (but let me know if you'd rather it go elsewhere).Implementation
Basically, strict_types only affects calling code and prevents scalar type coercion in the following cases:
CallLikes andAttributesFunctionLikesThis PR adds a new service class (
StrictTypeSafetyChecker) that iterates through all the above and checks the type compatibility. If the file is completely safe then the refactor is performed, otherwise the file is skipped. I conservatively skip files when we can't be sure (e.g., dynamic calls, unpacks, etc.).Tip
All of the actual type checking is deferred to phpstan's
$declaredType->accepts($valueType, strictTypes: true)api which really makes things clean on our end.CC: @TomasVotruba
Thanks!